package com.manager.container.controller;

import com.manager.common.customer.utils.RemoteApi;
import com.manager.common.ssh.RemoteExecuteLinuxCommandTools;
import com.manager.common.utils.DataTools;
import com.manager.common.vo.ResultVO;
import com.manager.model.DockerContainerStats;
import com.manager.model.FirstPageState;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/monitor")
public class DockerMonitorController {

    @RequestMapping("/getResourceInfoByContainerId")
    public ResultVO getResourceInfoByContainerId(String containerId) {
       try {
           String url = DataTools.getSystemSettingStringValByKey("get_container_real_time_info_by_id_url");
           JSONObject jsonObject = new JSONObject();
           jsonObject.put("containerId", containerId);
           ResultVO<JSONObject> resultVO = RemoteApi.executeHttp(url,jsonObject);
           return ResultVO.success(JSONObject.toBean(resultVO.getData(),DockerContainerStats.class));
       }catch (Exception e) {
           e.printStackTrace();
       }
       return ResultVO.failed();
    }

    @RequestMapping("/getMonitorInfoByContainerId")
    public ResultVO getMonitorInfoByContainerId(String containerId) {
        try {
            String url = DataTools.getSystemSettingStringValByKey("get_container_real_time_log_by_id_url");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("containerId", containerId);
            ResultVO<String> resultVO = RemoteApi.executeHttp(url,jsonObject);
            return resultVO;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/getDockerContainerTop")
    public ResultVO getDockerContainerTop(String containerId) {
        try {
            String url = DataTools.getSystemSettingStringValByKey("get_container_top_info_by_id_url");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("containerId", containerId);
            ResultVO<String> resultVO = RemoteApi.executeHttp(url,jsonObject);
            return resultVO;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/getLinuxBaseInfo")
    public ResultVO getLinuxBaseInfo() {
        try{
            String url = DataTools.getSystemSettingStringValByKey("get_linux_base_info_url");
            JSONObject jsonObject = new JSONObject();
            ResultVO<JSONArray> resultVO = RemoteApi.executeHttp(url,jsonObject);
            return resultVO;
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/saveContainerLogs")
    public ResultVO saveContainerLogs(String containerId) {
       try {
           String url = DataTools.getSystemSettingStringValByKey("docker_container_save_log_url");
           String tempSaveLogsPath = DataTools.getSystemSettingStringValByKey("docker.logs.save.temp.path");
           JSONObject jsonObject = new JSONObject();
           jsonObject.put("containerId", containerId);
           jsonObject.put("tempSavePath", tempSaveLogsPath);
           ResultVO<JSONArray> resultVO = RemoteApi.executeHttp(url,jsonObject);
            if(resultVO.hasSuccess()){
                List<String> out = new ArrayList<>();
                String basePath = DataTools.getSystemSettingStringValByKey("docker_container_logs_path");
                String localLogsDirPath = basePath + "/" + containerId ;
                String remoteFilePath = resultVO.getData().getString(0);
                boolean hasSuccess = RemoteExecuteLinuxCommandTools.downloadFileBySftp(remoteFilePath,localLogsDirPath,out);
                if(!hasSuccess){
                    return ResultVO.failed();
                }
                return ResultVO.success();
            }
           return ResultVO.failed();
       }catch (Exception e){
           e.printStackTrace();
       }
       return ResultVO.failed();
    }
}
